package xia;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.Toast;
import com.xuanfu_button.XuanfuButton_MapPointonLongClick;
import org_rosjava.ros.android.android_tutorial_map_viewer.My_map;
public class XuanfuButton_Click_point_menu {
static void del_point(final String pointTitle)
{
    int at_mark_jsondata_number=-1;
    for(int i=0;i<My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.size();i++)
    {
        String name=My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(i).name;
        if(name.equals(pointTitle))
        {
            at_mark_jsondata_number=i;
        }
    }
    if(at_mark_jsondata_number!=-1)
    {
        Float coordinate_0 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(0);
        Float coordinate_1 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(1);
        Float coordinate_2 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(2);
        Float coordinate_3 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(3);
        Float coordinate_4 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(4);
        Float coordinate_5 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(5);
        Float coordinate_6 = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).coordinate.get(6);
        String name = My_map.getinstance().mark_coordinatejsondata_last.mark_coordinate.get(at_mark_jsondata_number).name;
        final String coordinate_string = String.valueOf(coordinate_0) + "," + String.valueOf(coordinate_1) + "," + String.valueOf(coordinate_2) + "," + String.valueOf(coordinate_3) + "," + String.valueOf(coordinate_4) + "," + String.valueOf(coordinate_5) + "," + String.valueOf(coordinate_6) ;
        String data_value="mark:coordinate_del:"+coordinate_string+ ":" + pointTitle;
        My_map.getinstance().myRos_sendTextPublish.sendStringData(data_value);
    }
    else
    {
        Toast.makeText(My_map.getinstance(),"删除失败",Toast.LENGTH_LONG).show();
    }
}


    public static void showInputDialog_del_point(final String pointTitle) {
        final SelfDialog_d1 selfDialog;
        selfDialog = new SelfDialog_d1(My_map.getinstance());
        selfDialog.setTitle("确定要删除这个餐桌点吗？");
        selfDialog.setYesOnclickListener("确定", new SelfDialog_d1.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                //Toast.makeText(this,"点击了--确定--按钮",Toast.LENGTH_LONG).show();
                    if(My_map.getinstance().mark_coordinatejsondata_last!=null)
                    {
                        My_map.getinstance().mark_line_change_succeeded = false;
                        del_point(pointTitle);
                        selfDialog.dismiss();
                        My_map.getinstance().myMapView.rmMapPoint_xuanfu_mark();
                        My_map.getinstance().myMapView.rmMapPoint_tmp();
                        if (My_map.getinstance().isAdded) {
                            XuanfuButton_Create.removeXuanfuButtonView();
                            My_map.getinstance().isAdded = false;
                        }
                    }
                    else
                        Toast.makeText(My_map.getinstance(),"获取数据失败",Toast.LENGTH_LONG).show();
            }
        });
        selfDialog.setNoOnclickListener("取消", new SelfDialog_d1.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                My_map.getinstance().myMapView.rmMapPoint_xuanfu_mark();
                My_map.getinstance().myMapView.rmMapPoint_tmp();
                selfDialog.dismiss();
            }
        });
        selfDialog.show();

    }

    private static AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        }
    };
    private static PopupWindow.OnDismissListener dismissListener=new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
        }
    };

    public static void  init_xuanfuButton_PointonLongClick(final String pointTitle, final float downX, final float downY, final float fingerX, final float fingerY)
    {
        try {
            DisplayMetrics dm = new DisplayMetrics();
            My_map.getinstance().getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenWidth = dm.widthPixels;
            int screenHeight = dm.heightPixels;
            My_map.getinstance().xuanfuButton_PointonLongClick = new XuanfuButton_MapPointonLongClick(My_map.getinstance().getApplicationContext());
            My_map.getinstance().xuanfuButton_PointonLongClick.setButton1Text("删除标记");
            My_map.getinstance().xuanfuButton_PointonLongClick.setButton2Text("取消");
            int xuanfuWidth=0;
            int xuanfuHeight=0;
            if(My_map.getinstance().xuanfuButton_PointonLongClick.getlinearLayout_xuanfuWidth()!=0&&My_map.getinstance().xuanfuButton_PointonLongClick.getlinearLayout_xuanfuHeight()!=0) {
                xuanfuWidth = My_map.getinstance().xuanfuButton_PointonLongClick.getlinearLayout_xuanfuWidth();
                xuanfuHeight = My_map.getinstance().xuanfuButton_PointonLongClick.getlinearLayout_xuanfuHeight();
            }
            else
            {
                xuanfuWidth = 200;
                xuanfuHeight = 200;
            }
            My_map.getinstance().params_xuanfu = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            My_map.getinstance().params_xuanfu.width=xuanfuWidth;
            My_map.getinstance().params_xuanfu.height=xuanfuHeight;
            My_map.getinstance().xuanfu_x=(int)fingerX-xuanfuWidth;
            My_map.getinstance().xuanfu_y=(int)fingerY;
            if(My_map.getinstance().xuanfu_x<=0)
                My_map.getinstance().xuanfu_x=(int)fingerX+xuanfuWidth;
            if((fingerY+xuanfuHeight)>=screenHeight)
                My_map.getinstance().xuanfu_y=(int)fingerY-xuanfuHeight;
            My_map.getinstance().params_xuanfu.leftMargin=My_map.getinstance().xuanfu_x;
            My_map.getinstance().params_xuanfu.topMargin=My_map.getinstance().xuanfu_y;
            My_map.getinstance().xuanfuButton_PointonLongClick.button1().setOnClickListener(new View.OnClickListener() {//修改标记
                public void onClick(View v) {
                    if(My_map.getinstance().isAdded)
                    {
                        try {
                            ((ViewGroup)My_map.getinstance().xuanfuButton_PointonLongClick.getParent()).removeView(My_map.getinstance().xuanfuButton_PointonLongClick);
                        } catch (Exception e) {
                        }
                        My_map.getinstance().isAdded = false;
                    }
                    showInputDialog_del_point(pointTitle);
                }
            });
            My_map.getinstance().xuanfuButton_PointonLongClick.button2().setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    My_map.getinstance().myMapView.rmMapPoint_xuanfu_mark();
                    My_map.getinstance().myMapView.rmMapPoint_tmp();
                    if(My_map.getinstance().isAdded) {
                        try {
                            ((ViewGroup)My_map.getinstance().xuanfuButton_PointonLongClick.getParent()).removeView(My_map.getinstance().xuanfuButton_PointonLongClick);
                        } catch (Exception e) {
                        }
                        My_map.getinstance().isAdded = false;
                    }
                }
            });
        } catch (Exception e) {
        }
    }
}
